1730B - Meeting on the Line - CodeForces Solution


binary search brute force greedy implementation math ternary search two pointers

Please click on ads to support us..

Python Code:

def solve():
    n = int(input())
    x = [int(i) for i in input().split()]
    t = [int(i) for i in input().split()]
    l, r = 0, 1e10
    pos1, pos2 = 0, 1e8
    for i in range(n):
        l = max(l, t[i])
        pos1 = min(pos1, x[i])
        pos2 = max(pos2, x[i])
    eps = 1e-8
    m = 0
    while(r > l + eps):
        m = (l + r) / 2 
        p1,p2 = pos1, pos2
        for i in range(n):
            p1 = max(p1, x[i] - (m - t[i]))
            p2 = min(p2, x[i] + (m + t[i]))
        if p1 > p2:
            l = m 
        else:
            r = m 
    for i in range(n):
        pos1 = max(pos1, x[i] - (m - t[i]))
        pos2 = min(pos2, x[i] + (m - t[i]))
    x = (pos1 + pos2) / 2
    print('%.7f' % x)
T = int(input())
while(T):
    solve()
    T -= 1

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define fore(i, l, r) for (lli i = (l) - ((l) > (r)); i != (r) - ((l) > (r)); i += 1 - 2 * ((l) > (r)))
#define all(a) begin(a), end(a)
#define sz(a) lli(a.size())
#define pb push_back
#define f first
#define s second

#ifdef LOCAL
#include "../debug.h"
#else
#define debug(...)
#endif

using lli = long long;
using ld = long double;
using ii = pair<lli, lli>;

const lli N = 1e6 + 5;
const ld INF = 4e18 + 5;

const ld EPS = 1e-20;
#define eq(a, b) (abs((a) - (b)) <= +EPS)
#define neq(a, b) (!eq(a, b))
#define geq(a, b) ((a) - (b) >= -EPS)
#define leq(a, b) ((a) - (b) <= +EPS)
#define ge(a, b) ((a) - (b) > +EPS)
#define le(a, b) ((a) - (b) < -EPS)

lli n;
ii a[N];

template <class T>
bool umin(T& a, T b) {
  return (a = (le(a, b) ? a : b)) == b;
}

template <class T>
bool umax(T& a, T b) {
  return (a = (ge(a, b) ? a : b)) == b;
}

template <class T, class F>
pair<T, T> ternary(T lo, T hi, F f) {
  lli original_lo = lo;
  lli original_hi = hi;
  fore (it, 0, 234) {
    T m1 = lo + (hi - lo) / 3.0;
    T m2 = hi - (hi - lo) / 3.0;
    if (leq(f(m1), f(m2)))
      hi = m2;
    else
      lo = m1;
  }
  return min({make_pair(f(lo), lo), make_pair(f(hi), hi)});
}

void testCase() {
  cin >> n;
  multiset<lli> left, right;
  fore (i, 0, n) {
    cin >> a[i].f;
  }
  fore (i, 0, n) {
    cin >> a[i].s;
    right.insert(a[i].f + a[i].s);
  }
  ld ans = 0;
  ld mx = INF;
  ld prev = 0;
  ld delta = 0;
  sort(a, a + n);
  fore (i, 0, n) {
    delta += a[i].f - prev;
    prev = a[i].f;
    right.erase(right.find(a[i].f + a[i].s));
    left.insert(a[i].s - delta);
    lli mx_left = left.empty() ? 0 : *left.rbegin() + delta;
    lli mx_right = right.empty() ? 0 : *right.rbegin() - delta;
    if (umin<ld>(mx, max(mx_left, mx_right))) {
      ans = a[i].f;
    }
    if (i < n - 1 && a[i].f != a[i + 1].f) {
      debug(a[i], a[i + 1]);
      auto [best, x] = ternary<ld>(a[i].f, a[i + 1].f, [&](ld x) {
        ld cur_mx_left = mx_left + (x - a[i].f);
        ld cur_mx_right = mx_right - (x - a[i].f);
        ld mx = cur_mx_left;
        umax(mx, cur_mx_right);
        return mx;
      });
      if (umin(mx, best)) {
        debug(mx, best);
        ans = x;
      }
    }
  }
  cout << fixed << setprecision(10) << ans << '\n';
}

int main() {
  cin.tie(0)->sync_with_stdio(0), cout.tie(0);
  lli tc = 1;
  cin >> tc;
  while (tc--) {
    testCase();
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory